Previous Thread
Next Thread
Print Thread
Rate Thread
Joined: Dec 2000
Posts: 595
Member
Member
Offline
Joined: Dec 2000
Posts: 595
any quick code to discover if a file is an image, and if this image is either a jpg or a gif?

------------------
=^_^=

Sponsored Links
Joined: Sep 2000
Posts: 1,304
Addict
Addict
Offline
Joined: Sep 2000
Posts: 1,304
Right-click on the image then select properties???

------------------
The Fusion of Goku & Trunks...GoTorankusuKu


Joined: Dec 2000
Posts: 595
Member
Member
Offline
Joined: Dec 2000
Posts: 595
hehe perl

------------------
=^_^=

Joined: Aug 2000
Posts: 335
Member
Member
Offline
Joined: Aug 2000
Posts: 335
The Image::Size module at http://www.cpan.org/ can do this.

Joined: Dec 2000
Posts: 595
Member
Member
Offline
Joined: Dec 2000
Posts: 595
how? i mean, i read all the size.pm code but couldnt understand his (the autor) use of the $handle variable, wich he uses to compare different image types.

------------------
=^_^=

Sponsored Links
Joined: May 2000
Posts: 243
Member
Member
Offline
Joined: May 2000
Posts: 243
($file,$extention) = split(/./,$filename);

print "This file is not an image" if $extention ne 'gif' or $extention ne 'jpg';
print "This is a .jpg image" if $extention eq 'jpg';
print "This is a .gif image" if $extention eq 'gif';

That would do that trick

------------------
Powered by ZCom Bulletin Boards

Joined: Dec 2000
Posts: 595
Member
Member
Offline
Joined: Dec 2000
Posts: 595
yeah you are right..

but if the guy has some "." in the middle of the name of the file, or in the path of the file. this code is for use with an upload utility.

------------------
=^_^=

Joined: Aug 2000
Posts: 335
Member
Member
Offline
Joined: Aug 2000
Posts: 335
Here's an example. $image_file_fh is the file handle returned by CGI::upload(). The parameter to imgsize() can also be a file name.

Code
code:

If you want to check the file extension, you can ignore periods in the middle like this:

Code
code:

(Using 'die' in CGI scripts is generally not a good appproach. But these are just examples, and I wanted to keep them simple.)

This message has been edited by Dave_L on December 09, 2000 at 10:04 AM

Joined: Dec 2000
Posts: 595
Member
Member
Offline
Joined: Dec 2000
Posts: 595
great [Linked Image] does imagesize tests the code of the file to return $type, or just the filename

------------------
=^_^=

Joined: Aug 2000
Posts: 335
Member
Member
Offline
Joined: Aug 2000
Posts: 335
It reads the actual file. So if someone creates a .bmp file, and renames it to .jpg, Image::Size::imgsize() will correctly recognize that it's really a .bmp file.

Sponsored Links
Joined: Sep 2000
Posts: 755
P.I.T.A. / Programmer
P.I.T.A. / Programmer
Offline
Joined: Sep 2000
Posts: 755
Quote
quote:
That is entirely untrue.

--mark


"Annnnnnnndd now, opening for Iron Maiden...... WYLD STALLYNS!!!" --Bill S. Preston, Esquire and Ted "Theodore " Logan
Joined: Dec 2000
Posts: 595
Member
Member
Offline
Joined: Dec 2000
Posts: 595
thanks

------------------
=^_^=

Joined: Aug 2000
Posts: 335
Member
Member
Offline
Joined: Aug 2000
Posts: 335
Really? Why is that?

I use 'die' for serious errors that shouldn't ever occur, but for most error-checking, I try to do something more user-friendly.

Joined: Feb 2000
Posts: 4,625
Member
Member
Offline
Joined: Feb 2000
Posts: 4,625
In my current search CGI script, I use die. I find it more, well, helpful, than something more "user friendly" as you say.


------------------

Hack Developer of the Ultimate Bulletin Board.
Due to time limitation, I do not offer support Via. E-mail or AIM. Please post on the forums.

Regards,
MasterMind

Joined: Sep 2000
Posts: 755
P.I.T.A. / Programmer
P.I.T.A. / Programmer
Offline
Joined: Sep 2000
Posts: 755
Something user friendly as a matter of preference is fine, but that should not be presented as "generally not a good approach" which implies that there is something wrong with doing that.

Using $! in a die statement gives the error that was caused, so other then making the output page look pretty, there really isn't any thing different about doing that.

--mark


"Annnnnnnndd now, opening for Iron Maiden...... WYLD STALLYNS!!!" --Bill S. Preston, Esquire and Ted "Theodore " Logan
Joined: Feb 2000
Posts: 4,625
Member
Member
Offline
Joined: Feb 2000
Posts: 4,625
Yep. [Linked Image]

But thats not my preference.

------------------

Hack Developer of the Ultimate Bulletin Board.
Due to time limitation, I do not offer support Via. E-mail or AIM. Please post on the forums.

Regards,
MasterMind

Joined: Aug 2000
Posts: 335
Member
Member
Offline
Joined: Aug 2000
Posts: 335
I didn't mean to start a controversy with my 'die' comment [Linked Image]

Perhaps "generally not a good approach" was a poor choice of words.

If a script is intended for use by yourself, or other programmers, then a message like "Incorrect file type at line 123 myscript.cgi" may be fine. If the script is intended for use by Mr. Average Websurfer, then I would prefer to handle the error in a way that provides the user with information that he understands.

Just my opinion [Linked Image]


This message has been edited by Dave_L on December 09, 2000 at 06:36 PM

Joined: Oct 2000
Posts: 2,222
Veteran
Veteran
Offline
Joined: Oct 2000
Posts: 2,222
"I didn't mean to start a controversy with my 'die' comment"

But you didn't [Linked Image]

You did spark a discussion of methods and preferences, not a controversy. That would imply a different intent that is not present here. [Linked Image]

(hint on translating Markisms: Just because he is not wordy sometimes doesn't mean he was giving you a slapdown. He might just have been real busy at the time, when he IS giving you a slapdown it is usually painfully obvious)

"If a script is intended for use by yourself, or other programmers, then a message like "Incorrect file type at line 123 myscript.cgi" may be fine. If the script is intended for use by Mr. Average Websurfer, then I would prefer to handle the error in a way that provides the user with information that he understands."

That's what you did here:
die 'Image file type must be either .gif or .jpg';

Seems just as effective to me as spinning out a fancy webpage with the error message then exit; just not a nice looking.

howabout die 'Image file type must be either .gif or .jpg, hit your backbutton'; ?

You *could* always do it the UBB way, make a sub to pass messages like StandardHTML but for smaller scripts it seems like overkill to me.

I use die 'message'; in everything I write except the UBB. But then again all of the stuff I write for my own websites are a lot smaller, mail scripts, upload scripts, etc. Little nobranier 100 liner stuff. I don't see the need in getting more fancy for my crappy little websites [Linked Image] Your mileage will vary.

Anyhow, point was that "Perhaps "generally not a good approach" was a poor choice of words" was a correct statement. Using die 'message'; is perfectly acceptable depending on circumstances. maybe just not in yours [Linked Image]

------------------
"There is no spoon" is back.

This message has been edited by navaho on December 09, 2000 at 09:47 PM


Picture perfect penmanship here.
Joined: Sep 2000
Posts: 755
P.I.T.A. / Programmer
P.I.T.A. / Programmer
Offline
Joined: Sep 2000
Posts: 755
Tis, true... I wasn't meaning it as a controversy, just in that the way you had stated it gave the impression to folk that using die is a bad thing, and that goes against a lot of hard work i've put in trying to instill the notion of using it over the past X months [Linked Image]

--mark


"Annnnnnnndd now, opening for Iron Maiden...... WYLD STALLYNS!!!" --Bill S. Preston, Esquire and Ted "Theodore " Logan
Joined: Dec 2000
Posts: 595
Member
Member
Offline
Joined: Dec 2000
Posts: 595
yay ^^

upload custom avatar

thanks guys.

------------------
=^_^=


Link Copied to Clipboard
Donate Today!
Donate via PayPal

Donate to UBBDev today to help aid in Operational, Server and Script Maintenance, and Development costs.

Please also see our parent organization VNC Web Services if you're in the need of a new UBB.threads Install or Upgrade, Site/Server Migrations, or Security and Coding Services.
Recommended Hosts
We have personally worked with and recommend the following Web Hosts:
Shock Hosting
Stable Host
bluehost
InterServer
Visit us on Facebook
Member Spotlight
Gizmo
Gizmo
Portland, OR, USA
Posts: 5,834
Joined: January 2000
Forum Statistics
Forums63
Topics37,583
Posts293,955
Members13,824
Most Online151,614
Nov 14th, 2025
Today's Statistics
Currently Online 368
Topics Created 0
Posts Made 0
Users Online 0
Birthdays 24
Top Posters
AllenAyres 21,080
JoshPet 10,369
LK 7,394
Lord Dexter 6,708
Gizmo 5,834
Greg Hard 4,625
Top Posters(30 Days)
Top Likes Received
isaac 82
Gizmo 20
Brett 7
WebGuy 2
Top Likes Received (30 Days)
None yet
The UBB.Developers Network (UBB.Dev/Threads.Dev) is ©2000-2026 VNC Web Services

 
Powered by UBB.threads™ PHP Forum Software 8.1.0
(Snapshot build 20260108)